﻿using DataProcess.Encryption;
using MySqlTool;
using ParkSaleSDK.Adapter;
using ParkSaleSDK.Data;
using System.IO;

namespace ParkSaleSDK
{
    namespace MainSystem
    {
        public class MainSystem
        {
            public const string SignKey = "a9B86577ihdg7825";
            public const string DatabaseSettingPath = "Database.setting";
            public const int ParkOnSale = 1, ParkSoldOut = -1, ParkPreOrdered = 0;
            public const int Order_Success = 1, Order_Closed = -1, Order_Pending = 0,Order_NeedToPay=2;
            public static string SurveyModuleLocation = "Survey", ImageLocation = "Images", DiscountModuleLocation = "Discount", MessageModuleLocation = "Message";
            static DatabaseSetting setting;
            public static bool TokenAuth(string token)
            {
                token = MD5Coder.MD5Encrypt(token, System.Text.Encoding.UTF8);
                MySqlManager manager = new MySqlManager(setting.Server, setting.UserName, setting.Password, setting.Database, "SuUser", setting.Port);
                var result = manager.Find<Su>($"select * from SuUser where token=\"{token}\"");
                if (result.Count > 0)
                    return true;
                else
                    return false;
            }
            public static DatabaseSetting Database(string token)
            {
                if (TokenAuth(token))
                {
                    return setting;
                }
                else
                    return null;
            }
            public static void Init(DatabaseSetting database)
            {
                if (setting == null)
                {
                    setting = database;
                }
            }
        }

        public interface IConfig<T>
        {
            void Save(string path, T obj);
            T Read(string path);
        }

        [System.Serializable]
        public class DatabaseSetting : IConfig<DatabaseSetting>
        {
            public string Server
            {
                get;
                set;
            }
            public string Database
            {
                get;
                set;
            }
            public string UserName
            {
                get;
                set;
            }
            public string Password
            {
                get;
                set;
            }
            public int Port
            {
                get;
                set;
            }
            public DatabaseSetting(string server, string database, string user, string password, int port)
            {
                this.Server = server;
                this.Database = database;
                this.UserName = user;
                this.Password = password;
                this.Port = port;
            }
            public DatabaseSetting()
            {

                this.Server = "192.168.1.1";
                this.Database = "database";
                this.UserName = "root";
                this.Password = "password";
                this.Port = 3306;
            }

            public void Save(string path, DatabaseSetting obj)
            {
                using (FileStream file = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(DatabaseSetting));
                    serializer.Serialize(file, obj);
                }
            }

            public DatabaseSetting Read(string path)
            {
                DatabaseSetting setting = null;
                using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.ReadWrite))
                {
                    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(DatabaseSetting));
                    setting = (DatabaseSetting)serializer.Deserialize(file);
                }
                return setting;
            }
        }
    }


    namespace Modularity
    {

        public interface IModInfo
        {
            /// <summary>
            /// 模组名
            /// </summary>
            /// <returns></returns>
            string Name();
            /// <summary>
            /// 模组介绍
            /// </summary>
            /// <returns></returns>
            string About();
            /// <summary>
            /// 模组搜索代码
            /// </summary>
            /// <returns></returns>
            string Token();
        }
        /// <summary>
        /// 模块权限接口
        /// </summary>
        public interface IModSecurity
        {
            /// <summary>
            /// 超级用户代号
            /// </summary>
            /// <returns></returns>
            string SuToken();
        }
        /// <summary>
        /// 安全可访问数据枚举
        /// </summary>
        public enum DataNeedOption
        {
            Park, Order, Location, Company, Ad, User, Rights, UserOwnedHouse
        }
        /// <summary>
        /// 调研功能接口
        /// </summary>
        public interface ISurvey : IModInfo
        {
            /// <summary>
            /// 所需数据类型
            /// </summary>
            /// <returns></returns>
            DataNeedOption WhatINeed();
            /// <summary>
            /// 调研方法
            /// </summary>
            /// <param name="adp">受WhatINeed方法影响，根据枚举传入不同数据</param>
            /// <param name="requestArg"></param>
            /// <returns></returns>
            Data.SurveyData SurveyAction(ReadableDatabaseAdapter adp, object requestArg);
        }
        /// <summary>
        /// 优惠券功能接口
        /// </summary>
        public interface IDiscount : IModInfo
        {
            /// <summary>
            /// 优惠券可用性检查
            /// </summary>
            /// <param name="order">所用订单</param>
            /// <param name="houses">下单人所有房产</param>
            /// <param name="owned">下单人所有车库</param>
            /// <param name="arg">第三方自定义参数</param>
            /// <returns></returns>
            bool DiscountUseable(Order order, OwnedHouse[] houses, Park[] owned, object arg);
            /// <summary>
            /// 优惠券结算方法
            /// </summary>
            /// <param name="order">所用订单</param>
            /// <param name="houses">下单人所有房产</param>
            /// <param name="owned">下单人所有车库</param>
            /// <param name="arg">第三方自定义参数</param>
            /// <returns></returns>
            double FinalPrice(Order order, OwnedHouse[] houses, Park[] owned, object arg);

        }
        /// <summary>
        /// 消息功能接口
        /// </summary>
        public interface IMessage : IModInfo, IModSecurity
        {
            /// <summary>
            /// 处理信息方法
            /// </summary>
            /// <param name="message">信息内容</param>
            /// <param name="reply">用户信息回应</param>
            /// <returns>信息处理结果，大于1则信息已处理</returns>
            int HandleMessage(FullMessage message, object reply);
        }
    }
}
